//  ========== 
//  = Pies3D = 
//  ========== 
/*
 * 创建对象			var pies = Pies3D.createNew();
 * 添加已有svg的id 		pies.svgid("svgid");
 * 创建SVG			pies.CreateSvg;
 * 绘制				pies.Ldraw();
 * 属性设置：			pies.Sequence(a) 	a: 1:升序 	 0：无序 		-1：降序
 * 未来：
 * 		。。。
 * */
! function() {
	var Pies3D = {};

	//定义设置CreateNew方法
	Object.defineProperty(Pies3D, "CreateNew", {
		value: function() {
			return FCreateNew()
		}
	});

	function FCreateNew() {

		var pies3D = Object.create(Object);
		pies3D._switch = false;

		Object.defineProperty(pies3D, "Labelid", {
			value: function(v) {
				this._labelid = v;
				return this;
			}
		});

		Object.defineProperty(pies3D, "Legend_width", {
			value: function(v) {
				this._legend_width = v;
				return this;
			}
		});

		Object.defineProperty(pies3D, "Pies_height", {
			value: function(v) {
				this._pies_height = v;
				return this;
			}
		});

		Object.defineProperty(pies3D, "Pies_angle", {
			value: function(v) {
				this._pies_angle = v;
				return this;
			}
		});

		Object.defineProperty(pies3D, "Pies_outerRadius", {
			value: function(v) {
				this._pies_outerRadius = v;
				return this;
			}
		});

		Object.defineProperty(pies3D, "Pies_innerRadius", {
			value: function(v) {
				this._pies_innerRadius = v;
				return this;
			}
		});

		Object.defineProperty(pies3D, "Linkages", {
			value: function(v) {
				this._linkages = v;
				return this;
			}
		});

		Object.defineProperty(pies3D, "ColorScale", {
			value: function(v) {
				this._colorScale = v;
				return this;
			}
		});
		var sequence_value;
		Object.defineProperty(pies3D, "_sequence", {
			get: function() {
				return sequence_value;
			},
			set: function(v) {
				sequence_value = v;
			}
		});

		Object.defineProperty(pies3D, "Sequence", {
			value: function(v) {
				this._sequence = v;
				return this;
			}
		});

		//定义_pies_x的属性
		var pies_x_value;
		Object.defineProperty(pies3D, "_pies_x", {
			get: function() {
				return pies_x_value;
			},
			set: function(v) {
				pies_x_value = v;
			}
		});

		//定义设置_pies_x属性的方法
		Object.defineProperty(pies3D, "Pies_x", {
			value: function(v) {
				this._pies_x = v;
				return this;
			}
		});

		//定义_pies_y的属性
		var pies_y_value;
		Object.defineProperty(pies3D, "_pies_y", {
			get: function() {
				return pies_y_value;
			},
			set: function(v) {
				pies_y_value = v;
			}
		});

		//定义设置_pies_y属性的方法
		Object.defineProperty(pies3D, "Pies_y", {
			value: function(v) {
				this._pies_y = v;
				return this;
			}
		});
		var data_value;
		Object.defineProperty(pies3D, "_data", {
			get: function() {
				return data_value;
			},
			set: function(v) {
				data_value = v;
			}
		});

		Object.defineProperty(pies3D, "Data", {
			value: function(v) {
				this._data = v;
				return this;
			}
		});

		Object.defineProperty(pies3D, "CreateSvg", {
			value: function() {
				DetermineProperties(this);
				return this;
			}
		});
		Object.defineProperty(pies3D, "Ldraw", {
			value: function() {
				Detect_function(pies3D._data) ?
					(alert("设置成功"), console.log("Pies3D未指定data")) :
					(Tool_Pies3D(this), //全局工具
						DataPretreatment_Pies3D(this), //数据预处理
						Draw_Pies3D(this)); //绘制吧
				return this;
			}
		})

		//		//定义_sort的属性
		//		var sort_value;
		//		Object.defineProperty(Pies3D, "_sort", {
		//		    get: function() {
		//		        return  sort_value;
		//		    },
		//		    set: function (v) {
		//		        sort_value = v;
		//		    }
		//		});
		//		                
		//		//定义设置_sort属性的方法
		//		Object.defineProperty(Pies3D, "Sort", {
		//		    value: function(v) {
		//		        this._sort = v ;
		//		        return this;
		//		        }
		//		});
		//  ========== 
		//  = 判断属性 = 
		//  ========== 
		DetermineProperties = function(a) {
			Detect_function(a._labelid) ?
				console.log("请添加svg父标签id用于添加svg") :
				d3.select("#" + pies3D._labelid)[0][0] == null ?
				console.log("请检查id是否正确") :
				SetId(a);
			a._width = $("#" + a._labelid).width();
			a._legend_width = Detect_function(a._legend_width) ? 100 : a._legend_width;
			a._pies_width = a._width - a._legend_width;
			a._height = a._width;
			a._pies_height = Detect_function(a._pies_height) ? 15 : a._pies_height;
			a._pies_angle = Detect_function(a._pies_angle) ? 45 : a._pies_angle;

			a._pies_outerRadius = Detect_function(a._pies_outerRadius) ? 0 : a._pies_outerRadius;
			a._pies_outerRadius2 = a._pies_width - a._pies_outerRadius;
			a._pies_innerRadius = Detect_function(a._pies_innerRadius) ? 0.618 : a._pies_innerRadius;
			a._pies_rx = a._pies_outerRadius2 / 2;
			a._pies_ry = a._pies_rx * Math.cos(a._pies_angle * Math.PI / 180);
			a._pies_x = Detect_function(a._pies_x) ? a._pies_width / 2 : a._pies_x;
			a._pies_y = Detect_function(a._pies_y) ? a._pies_width / 2 : a._pies_width / 2 - a._pies_outerRadius / 2 + a._pies_y;
			//			a._linkages = 
			a._colorScale = Detect_function(a._colorScale) ? d3.scale.category20() : a._colorScale;

			a._switch = false;
			//			a._sort = Detect_function(a._sort) ? null : a._sort;
			CreateSvg_Pies3D(a)
		}

		Detect_function = function(object_lh) {
			return typeof(object_lh) == "undefined";
		}

		CreateSvg_Pies3D = function(pies3D) {
			pies3D.svg = d3.select("#" + pies3D._labelid).append("svg")
				.attr("id", pies3D._svgid)
				.attr("preserveAspectRatio", "xMidYMid meet")
				.attr("viewBox", "0 0 " + pies3D._width + " " + pies3D._height + " ");
			pies3D.piesg = pies3D.svg
				.append("g")
				.attr("transform", "translate(" + pies3D._pies_x + "," + pies3D._pies_y + ")")
				.attr("id", pies3D._pies3D_g_id);
			pies3D.legendg = pies3D.svg
				.append("g")
				.attr("transform", "translate(" + pies3D._pies_width + ",0)")
				.attr("id", pies3D._legend_g_id);
			pies3D.tooltip = d3.select("#" + pies3D._labelid)
				.append("div")
				.attr("class", pies3D._tooltip_class)
				.attr("left", 0)
				.attr("top", pies3D._height)
				.style("opacity", 0);
		}

		function getType(o) {
			var _t;
			return((_t = typeof(o)) == "object" ? o == null && "null" || Object.prototype.toString.call(o).slice(8, -1) : _t).toLowerCase();
		}

		function extend(destination, source) {
			for(var p in source) {
				console.log(p);
				if(getType(source[p]) == "array" || getType(source[p]) == "object") {
					destination[p] = getType(source[p]) == "array" ? [] : {};
					arguments.callee(destination[p], source[p]);
				} else {
					destination[p] = source[p];
				}
			}
		}
		DataPretreatment_Pies3D = function(pies3D) {
			var pie_data, pie2_data;
			pies3D._data2 = [];
			console.log(pies3D._data);
			pies3D._data[pies3D._data.length - 1][0] == "更多..." ? (console.log("yougengduo"),
					extend(pies3D._data2, pies3D._data),
					console.log(pies3D._data),
					console.log(pies3D._data2),
					pies3D._data2.pop(),
					console.log(pies3D._data),
					console.log(pies3D._data2),
					pie_data = pies3D.layoutPie(pies3D._data),
					pies3D.layoutPie.endAngle(7 / 2 * Math.PI - (pie_data[pie_data.length - 1].endAngle - pie_data[pie_data.length - 1].startAngle)),
					console.log(pie_data[pie_data.length - 1], pie_data),
					pie2_data = pies3D.layoutPie(pies3D._data2),
					console.log(pie2_data), pie_data[pie_data.length - 1].startAngle = 7 / 2 * Math.PI - (pie_data[pie_data.length - 1].endAngle - pie_data[pie_data.length - 1].startAngle), pie_data[pie_data.length - 1].endAngle = 7 / 2 * Math.PI,
					console.log(pie_data[pie_data.length - 1], pie_data),
					pie2_data.push(pie_data[pie_data.length - 1]),
					pies3D._pies_data = pie2_data, //pie2_data.push(pie_data[pie_data.length - 1]),
					console.log(pies3D._pies_data)
				) :
				pies3D._pies_data = pies3D.layoutPie(pies3D._data);
			//			pies3D._pies_data = function() {
			//				return d3.layout.pie()
			//					.sort(/*Pies3D_Sequence(pies3D._sequence)*/pies3D._sequence)
			//					.value(function(d) {
			//						return d[1];
			//					})(pies3D._data)
			//			}();
			console.log(pies3D._pies_data[pies3D._pies_data.length-1].startAngle);
			console.log(Math.sin(pies3D._pies_data[pies3D._pies_data.length-1].startAngle ),pies3D._pies_data[pies3D._pies_data.length-1].endAngle -  (3/2*Math.PI).toFixed(16));
			console.log(pies3D._pies_data[pies3D._pies_data.length-1].endAngle - pies3D._pies_data[pies3D._pies_data.length-1].startAngle - 2* Math.PI);
			pies3D._legend_data = function() {
				var i = 0;
				var Data1 = [];
				for(i; i < pies3D._data.length; i++) {

					if(pies3D._data[i][1] != "0") {
						Data1.push(pies3D._data[i]);
					}
				};
				return Data1;
			}();
			for(var i = 0; i < pies3D._legend_data.length; i++) {
				pies3D._colorScale(pies3D._legend_data[0]);
			}

			//		pies3D._legend_data =
		}

		Tool_Pies3D = function(pies3D) {
			console.log(pies3D._sequence);
			pies3D._sequence = Detect_function(pies3D._sequence) ? null : pies3D._sequence;
			console.log(pies3D._sequence);
			pies3D._arc = d3.svg.arc()
				.innerRadius(pies3D._innerRadius)
				.outerRadius(pies3D._outerRadius);
			pies3D.layoutPie = d3.layout.pie()
				.sort(Pies3D_Sequence(pies3D._sequence))
				.startAngle(Math.PI * 3 / 2)
				.endAngle(7 / 2 * Math.PI)
				.value(function(d) {
					console.log(d[0], parseInt(d[1]));
					return parseInt(d[1]);
				});
			console.log(pies3D._sequence);
			console.log(pies3D.layoutPie);
		}
		Draw_Pies3D = function(pies3D) {
			pies3D._switch = true;
			pies3D.pies_innerSlice = pies3D.piesg.selectAll("." + pies3D._pie3D_innerSlice_class);
			var pies_innerSlice_updata = pies3D.pies_innerSlice.data(pies3D._pies_data);
			var pies_innerSlice_enter = pies_innerSlice_updata.enter();
			var pies_innerSlice_exit = pies_innerSlice_updata.exit();

			pies3D.pies_topSlice = pies3D.piesg.selectAll("." + pies3D._pie3D_topSlice_class);
			var pies_topSlice_updata = pies3D.pies_topSlice.data(pies3D._pies_data);
			var pies_topSlice_enter = pies_topSlice_updata.enter();
			var pies_topSlice_exit = pies_topSlice_updata.exit();

			pies3D.pies_outerSlice = pies3D.piesg.selectAll("." + pies3D._pie3D_outerSlice_class);
			var pies_outerSlice_updata = pies3D.pies_outerSlice.data(pies3D._pies_data);
			var pies_outerSlice_enter = pies_outerSlice_updata.enter();
			var pies_outerSlice_exit = pies_outerSlice_updata.exit();

			pies3D.legend_rect = pies3D.legendg.selectAll("rect");
			var pies_legend_rect_updata = pies3D.legend_rect.data(pies3D._legend_data);
			var pies_legend_rect_enter = pies_legend_rect_updata.enter();
			var pies_legend_rect_exit = pies_legend_rect_updata.exit();

			pies3D.legend_text = pies3D.legendg.selectAll("text");
			var pies_legend_text_updata = pies3D.legend_text.data(pies3D._legend_data);
			var pies_legend_text_enter = pies_legend_text_updata.enter();
			var pies_legend_text_exit = pies_legend_text_updata.exit();
			pies_innerSlice_updata
				.transition()
				.duration(750)
				.style("fill", function(d) {
					return d.data[0] == "更多..." ? d3.hsl("#DCDCDC").darker(0.7) : d3.hsl(pies3D._colorScale(d.data[0])).darker(0.7);
				})
				.attrTween("d", arcTweenInner);
			pies_innerSlice_enter
				.append("path").attr("class", pies3D._pie3D_innerSlice_class)
				.style("fill", function(d) { //#DCDCDC
					return d.data[0] == "更多..." ? d3.hsl("#DCDCDC").darker(0.7) : d3.hsl(pies3D._colorScale(d.data[0])).darker(0.7);
				})
				.attr("d", function(d) {
					return pieInner(d, 0, 0, 0, 0);
				})
				.each(function(d) {
					this._current = d;
				})
				.on("click", function(d) {
					var idg = d3.select(this).node().parentNode.parentNode.parentNode.id;
					click_pies(d, idg)
				})
				.on("mouseover", function(d) {
					mouseover_pies(d, pies3D);
					/* 鼠标移动时，更改样式 left 和 top 来改变提示框的位置 */

					//				tooltip.style("left", (d3.event.pageX) + "px")
					//					.style("top", (d3.event.pageY + 20) + "px");
				})
				.on("mouseout", function(d) {
					/* 鼠标移出时，将透明度设定为0.0（完全透明）*/

					//				tooltip.style("opacity", 0.0);
				})
				.on("mouseout", function(d, i) {
					mouseout_pies(d, pies3D);
					//					var idg = d3.select(this).node().parentNode.parentNode.parentNode.id;
				})
				.transition().duration(2000)
				.attr("d", function(d) {
					return pieInner(d, pies3D._pies_rx + 0.5, pies3D._pies_ry + 0.5, pies3D._pies_height, pies3D._pies_innerRadius);
				});
			pies_innerSlice_exit.remove();

			pies_topSlice_updata
				.transition()
				.duration(750)
				.style("fill", function(d) {
					return d.data[0] == "更多..." ? "#DCDCDC" : pies3D._colorScale(d.data[0]);
				})
				.attrTween("d", arcTweenTop);
			pies_topSlice_enter
				.append("path")
				.attr("class", pies3D._pie3D_topSlice_class)
				.attr("id", function(d, i) {
					return pies3D._pie3D_topSlice_id + i;
				})
				.style("fill", function(d) {
					return d.data[0] == "更多..." ? "#DCDCDC" : pies3D._colorScale(d.data[0]);
				})
				//				.style("stroke", function(d) {
				//					return pies3D._colorScale(d.data[0]);
				//				})
				.attr("d", function(d) {
					return pieTop(d, 0, 0, 0);
				})
				.classed("active", false)
				.each(function(d) {
					this._current = d;
				})
				.on("click", function(d, i) {
					var idg = d3.select(this).node().parentNode.parentNode.parentNode.id;
					click_pies(d, idg)
				})
				.on("mousemove", function(d) {
					mouseover_pies(d, pies3D);
					/* 鼠标移动时，更改样式 left 和 top 来改变提示框的位置 */

					//				tooltip.style("left", (d3.event.pageX) + "px")
					//					.style("top", (d3.event.pageY + 20) + "px");
				})
				.on("mouseout", function(d) {
					/* 鼠标移出时，将透明度设定为0.0（完全透明）*/

					//				tooltip.style("opacity", 0.0);
				})
				.on("mouseout", function(d, i) {
					mouseout_pies(d, pies3D);
					//					var idg = d3.select(this).node().parentNode.parentNode.parentNode.id;
				})
				.transition().duration(2000)
				.attr("d", function(d) {
					return pieTop(d, pies3D._pies_rx, pies3D._pies_ry, pies3D._pies_innerRadius);
				});

			pies_topSlice_exit.remove();

			pies_outerSlice_updata
				.transition()
				.duration(750)
				.style("fill", function(d) {
					return d.data[0] == "更多..." ? d3.hsl("#DCDCDC").darker(0.4) : d3.hsl(pies3D._colorScale(d.data[0])).darker(0.4);
				})
				.attrTween("d", arcTweenOuter);
			pies_outerSlice_enter
				.append("path").attr("class", pies3D._pie3D_outerSlice_class)
				.style("fill", function(d) {
					return d.data[0] == "更多..." ? d3.hsl("#DCDCDC").darker(0.4) : d3.hsl(pies3D._colorScale(d.data[0])).darker(0.4);
				})
				.each(function(d) {
					this._current = d;
				})
				.attr("d", function(d) {
					return pieOuter(d, 0, 0, 0);
				})
				.on("click", function(d, i) {
					var idg = d3.select(this).node().parentNode.parentNode.parentNode.id;
					click_pies(d, idg)
				})
				.on("mousemove", function(d, i) {
					mouseover_pies(d, pies3D);
					/* 鼠标移动时，更改样式 left 和 top 来改变提示框的位置 */
					/*.style("visibility","no-hidden")*/
					;
					//				tooltip.style("left", (d3.event.pageX) + "px")
					//					.style("top", (d3.event.pageY + 20) + "px");
				})
				.on("mouseout", function(d) {
					/* 鼠标移出时，将透明度设定为0.0（完全透明）*/

					//				tooltip.style("opacity", 0.0);
				})
				.on("mouseout", function(d, i) {
					mouseout_pies(d, pies3D);
					//					var idg = d3.select(this).node().parentNode.parentNode.parentNode.id;
				})
				.transition().duration(2000)
				.attr("d", function(d) {
					return pieOuter(d, pies3D._pies_rx - .5, pies3D._pies_ry - .5, pies3D._pies_height);
				});
			pies_outerSlice_exit.remove();
			//_pie3D_percent_class
			//			pies3D.pies_percent = pies3D.piesg.selectAll("." + pies3D._pie3D_percent_class).data(pies3D._pies_data).enter().append("text").attr("class", pies3D._pie3D_percent_class)
			//				.attr("x", function(d) {
			//					return 0.8 * pies3D._pies_rx * Math.cos(0.5 * (d.startAngle + d.endAngle));
			//				})
			//				.attr("y", function(d) {
			//					return 0.9 * pies3D._pies_ry * Math.sin(0.5 * (d.startAngle + d.endAngle));
			//				})
			//				.text(getPercent).each(function(d) {
			//					this._current = d;
			//				})
			//				.style("visibility", "hidden")
			//				//				.style("color","red")
			//				//				.style("color","white")
			//				.on("click", function(d, i) {
			//					var idg = d3.select(this).node().parentNode.parentNode.parentNode.id;
			//					click_pies(d, idg)
			//				})
			//				.on("mouseover", function(d, i) {
			//					mouseover_pies(d, pies3D);
			//					//					var idg = d3.select(this).node().parentNode.parentNode.parentNode.id;
			//				})
			//				.on("mouseout", function(d, i) {
			//					mouseout_pies(d, pies3D);
			//					//					var idg = d3.select(this).node().parentNode.parentNode.parentNode.id;
			//				});
			//legend
			//添加矩形
			pies_legend_rect_updata
				.transition().duration(750)
				.style("fill", function(d) {

					return d[0] == "更多..." ? "#DCDCDC" : pies3D._colorScale(d[0]);
				});
			pies_legend_rect_enter
				.append("rect")
				.attr("class", pies3D._legend_rect_class)
				.attr("x", 0)
				.attr("y", function(d, i) {
					return i * 15;
				})
				.attr("fill", function(d) {
					return d[0] == "更多..." ? "#DCDCDC" : pies3D._colorScale(d[0]);
				})
				.attr("width", 15)
				.attr("height", 15)
				.on("click", function(d, i) {
					var idg = d3.select(this).node().parentNode.parentNode.parentNode.id;
					click_legend(d, idg)
				})
				.on("mouseover", function(d, i) {
					mouseover_legend(d, pies3D, i);
					//					var idg = d3.select(this).node().parentNode.parentNode.parentNode.id;
				})
				.on("mouseout", function(d, i) {
					mouseout_legend(d, pies3D)
						//					var idg = d3.select(this).node().parentNode.parentNode.parentNode.id;
				});
			pies_legend_rect_exit.remove();
			//			.attr("transform", "translate(100 ,0)");
			pies_legend_text_updata
				.transition()
				.duration(750)
				.text(function(d) {
					return d[0];
				});
			pies_legend_text_enter
				.append("text")
				.attr("class", pies3D._legend_text_class)
				.attr("class", "ceshitext")
				.text(function(d) {
					return d[0].length > 5 ?
						d[0].substring(0, 5) + "..." : d[0];
				})
				.attr("x", 0)
				.attr("y", function(d, i) {
					return i * 15 - 3;
				})
				//				.attr("textLength", "80px")
				.attr("transform", "translate(18 ,15)")
				.attr("fill", function(d) {
					return d[0] == "更多..." ? "#DCDCDC" : "black"
				})
				//				.style("font-size", "0.7vw")
				//				.style("font-size", "12px")
				.attr("onselectstart", function(event) {
					return "return false";
				})
				.attr("onmousedown", function(event) {

					return "return false";
				})
				.attr("ondragstart", function(event) {
					return "return false";
				})
				.attr("mousedown", function(event) {
					return "retrun false";
				})

			//				.style({
			//					"text-overflow": "clip",
			//					"-webkit-box-orient": "vertical",
			//					"-webkit-line-clamp": "2",
			//					"padding-left": "15px",
			//					"padding-right":"15px",
			//					"box-sizing": "border-box" 
			//				})
			.on("click", function(d, i) {

					var idg = d3.select(this).node().parentNode.parentNode.parentNode.id;
					click_legend(d, idg)
				})
				.on("mouseover", function(d, i) {
					mouseover_legend(d, pies3D, i);
					//					var idg = d3.select(this).node().parentNode.parentNode.parentNode.id;
				})
				.on("mouseout", function(d, i) {
					mouseout_legend(d, pies3D);
					//					var idg = d3.select(this).node().parentNode.parentNode.parentNode.id;
				});
			pies_legend_text_exit.remove();
			/*禁止文本选中*/
			$("#" + pies3D._legend_text_class).click(function(event) {
				event.preventDefault();
			});

			function arcTweenInner(a) {
				var i = d3.interpolate(this._current, a);
				this._current = i(0);
				return function(t) {
					return pieInner(i(t), pies3D._pies_rx + 0.5, pies3D._pies_ry + 0.5, pies3D._pies_height, pies3D._pies_innerRadius);
				};
			}

			function arcTweenTop(a) {
				var i = d3.interpolate(this._current, a);
				this._current = i(0);
				return function(t) {
					return pieTop(i(t), pies3D._pies_rx, pies3D._pies_ry, pies3D._pies_innerRadius);
				};
			}

			function arcTweenOuter(a) {
				var i = d3.interpolate(this._current, a);
				this._current = i(0);
				return function(t) {
					return pieOuter(i(t), pies3D._pies_rx - .5, pies3D._pies_ry - .5, pies3D._pies_height);
				};
			}

			function textTweenX(a) {
				var i = d3.interpolate(this._current, a);
				this._current = i(0);
				return function(t) {
					return 0.6 * pies3D._pies_rx * Math.cos(0.5 * (i(t).startAngle + i(t).endAngle));
				};
			}

			function textTweenY(a) {
				var i = d3.interpolate(this._current, a);
				this._current = i(0);
				return function(t) {
					return 0.6 * pies3D._pies_rx * Math.sin(0.5 * (i(t).startAngle + i(t).endAngle));
				};
			}
		}

		function SetId(a) {
			a._svgid = a._labelid + "Svg";
			a._pies3D_g_id = a._labelid + "Pies3Dg";
			//			a._button_g_id = a._labelid + "Buttong";
			//			a._button_id = a._labelid + "Butto";
			a._legend_g_id = a._labelid + "Legendg";
			a._pie3D_innerSlice_class = a._labelid + "_pie3D_innerSlice";
			a._pie3D_topSlice_class = a._labelid + "_pie3D_topSlice";
			a._pie3D_topSlice_id = a._labelid + "_pie3D_topSliceid";
			a._pie3D_outerSlice_class = a._labelid + "_pie3D_outerSlice";
			//			a._pie3D_percent_class = a._labelid + "_pie3D_percent";
			a._legend_rect_class = a._labelid + "Legend_rect";
			a._legend_text_class = a._labelid + "Legend_text";
			a._tooltip_class = "PiesTooltip";

		}
		/*绘制部分
			 自定义绘制3D图形
			 所以各面单独绘制*/
		function pieTop(d, rx, ry, ir) {
			console.log(d.startAngle,d.endAngle);

			if(d.endAngle - d.startAngle == 0) return "M 0 0";
			
			var cossx,sinsy,cosex,siney;
			cossx = Math.cos(d.startAngle),sinsy = Math.sin(d.startAngle),cosex = Math.cos(d.endAngle),siney = Math.sin(d.endAngle);
			if (Math.cos(d.startAngle).toFixed(4) == 0 && Math.cos(d.endAngle).toFixed(4) == 0) {
			    cossx = 0,cosex = -0.0001;
			}
			console.log(Math.cos( d.startAngle),Math.sin(d.startAngle),Math.cos(d.endAngle),Math.sin(d.endAngle));
			console.log(cossx,sinsy,cosex,siney);
			var sx = rx * cossx,
				sy = ry * sinsy ,
				ex = rx * cosex,
				ey = ry * siney;
				
console.log(Math.cos( d.startAngle));
console.log( Math.sin( d.startAngle));
console.log(Math.cos( d.endAngle));
console.log(Math.sin( d.endAngle).toFixed(2));
console.log(sx,sy,ex,ey);
			var ret = [];
			ret.push("M", sx, sy, "A", rx, ry, "0", (d.endAngle - d.startAngle > Math.PI ? 1 : 0), "1", ex, ey, "L", ir * ex, ir * ey);
			ret.push("A", ir * rx, ir * ry, "0", (d.endAngle - d.startAngle > Math.PI ? 1 : 0), "0", ir * sx, ir * sy, "z");
			console.log(ret.join(" "));
			return ret.join(" ");
		}

		function pieOuter(d, rx, ry, h) {

			var startAngle = d.startAngle < 2 * Math.PI ? 2 * Math.PI : (d.startAngle > 3 * Math.PI ? 3 * Math.PI : d.startAngle);
			//			var startAngle = d.startAngle > 3 * Math.PI ? Math.PI : d.startAngle;
			var endAngle = d.endAngle < 2 * Math.PI ? 2 * Math.PI : (d.endAngle > 3 * Math.PI ? 3 * Math.PI : d.endAngle);
			var sx = rx * Math.cos(startAngle),
				sy = ry * Math.sin(startAngle),
				ex = rx * Math.cos(endAngle),
				ey = ry * Math.sin(endAngle);
			//
			var ret = [];
			ret.push("M", sx, h + sy, "A", rx, ry, "0 0 1", ex, h + ey, "L", ex, ey, "A", rx, ry, "0 0 0", sx, sy, "z");
			return ret.join(" ");
		}

		function pieInner(d, rx, ry, h, ir) {
			var startAngle, endAngle, startAngle2, endAngle2, ret = [],
				sx, sy, ex, ey, sx2, sy2, ex2, ey2;
			d.startAngle < 2 * Math.PI ? a(d) : b(d);

			function a(d) {
				startAngle = d.startAngle
				d.endAngle < 2 * Math.PI ? (endAngle = d.endAngle, c()) : (d.endAngle < 3 * Math.PI ? (endAngle = 2 * Math.PI, c()) : (endAngle = 2 * Math.PI, startAngle2 = 3 * Math.PI, endAngle2 = d.endAngle, d1()))
				function c() {
					sx = ir * rx * Math.cos(startAngle),
						sy = ir * ry * Math.sin(startAngle),
						ex = ir * rx * Math.cos(endAngle),
						ey = ir * ry * Math.sin(endAngle);
					ret.push("M", sx, sy, "A", ir * rx, ir * ry, "0 0 1", ex, ey, "L", ex, h + ey, "A", ir * rx, ir * ry, "0 0 0", sx, h + sy, "z");
				}

				function d1() {
					sx = ir * rx * Math.cos(startAngle),
						sy = ir * ry * Math.sin(startAngle),
						ex = ir * rx * Math.cos(endAngle),
						ey = ir * ry * Math.sin(endAngle);
					sx2 = ir * rx * Math.cos(startAngle2),
						sy2 = ir * ry * Math.sin(startAngle2),
						ex2 = ir * rx * Math.cos(endAngle2),
						ey2 = ir * ry * Math.sin(endAngle2);
					ret.push("M", sx, sy, "A", ir * rx, ir * ry, "0 0 1", ex, ey, "L", ex, h + ey, "A", ir * rx, ir * ry, "0 0 0", sx, h + sy, "z", "M", sx2, sy2, "A", ir * rx, ir * ry, "0 0 1", ex2, ey2, "L", ex2, h + ey2, "A", ir * rx, ir * ry, "0 0 0", sx2, h + sy2, "z");
				}
			}

			function b(d) {
				d.startAngle < 3 * Math.PI ? startAngle = 3 * Math.PI : startAngle = d.startAngle
					//    			 startAngle = d.startAngle < 2 * Math.PI ? d.startAngle : (d.startAngle < 3 * Math.PI ? 3 * Math.PI : d.startAngle);
				endAngle = d.endAngle < 2 * Math.PI ? d.endAngle : (d.endAngle < 3 * Math.PI ? 3 * Math.PI : d.endAngle);
				sx = ir * rx * Math.cos(startAngle),
					sy = ir * ry * Math.sin(startAngle),
					ex = ir * rx * Math.cos(endAngle),
					ey = ir * ry * Math.sin(endAngle);
				ret.push("M", sx, sy, "A", ir * rx, ir * ry, "0 0 1", ex, ey, "L", ex, h + ey, "A", ir * rx, ir * ry, "0 0 0", sx, h + sy, "z");
			}
			return ret.join(" ");
		}

		Pies3D_Sequence = function(sequence) {
			return sequence == 1 ? function comparator(a, b) {
					return d3.ascending(parseInt(a[1]), parseInt(b[1])); //升序
				} :
				(sequence == -1 ? function comparator(a, b) {
					return d3.descending(parseInt(a[1]), parseInt(b[1])); //降序 
				} : null)
		}

		function click_pies(p, idg) {
			console.log(p.data[0]);
			p.data[0] == "更多..." || ClickThePiesRequest(idg, p.data[0]);

		}

		function click_legend(p, idg) {
			p[0] == "更多..." || ClickThePiesRequest(idg, p[0]);

		}

		function mouseover_pies(d, pies3D) {
			pies3D.tooltip.html(d.data[0] + "<br />" + "数量：" + d.value + "<br />" +
					"百分比：" + Math.round(1000 * (d.endAngle - d.startAngle) / (Math.PI * 2)) / 10 + '%')
				.style("left", (d3.event.pageX) + "px")
				.style("top", (d3.event.pageY + 20) + "px")
				.style("opacity", 1.0);
			pies3D.svg.selectAll("." + pies3D._pie3D_topSlice_class)
				.classed("active", function(p) {
					return p.data[0] === d.data[0] //|| p.data[0] === d; 
				});
			pies3D.svg.selectAll("." + pies3D._pie3D_innerSlice_class)
				.classed("active", function(p) {
					return p.data[0] === d.data[0] //|| p.data[0] === d; 
				});
			//			pies3D.svg.selectAll("." + pies3D._pie3D_outerSlice_class)
			//				.classed("active", function(p) {
			//					return p.data[0] === d.data[0] //|| p.data[0] === d; 
			//				});
			//			pies3D.svg.selectAll("." + pies3D._pie3D_percent_class)
			//				.style("visibility", function(p) { //.style("visibility", "hidden")
			//					return p.data[0] === d.data[0] ? "visible" : "hidden" //|| p.data[0] === d; 
			//				})
			//				.style("color", function(p) {
			//					return p.data[0] === d[0] ? "white" : "none";
			//				});
		}

		function mouseover_legend(d, pies3D, i) {
			pies3D.tooltip.html(d[0])
				.style("left", (d3.event.pageX) + "px")
				.style("top", (d3.event.pageY) + 5 + "px")
				.style("opacity", 1.0);
			pies3D.svg.selectAll("#" + pies3D._pie3D_topSlice_id + i)
				.classed("active", true);
			pies3D.svg.selectAll("." + pies3D._pie3D_innerSlice_class)
				.classed("active", function(p) {
					return p.data[0] === d[0] //|| p.data[0] === d; 
				});
			//			pies3D.svg.selectAll("." + pies3D._pie3D_outerSlice_class)
			//				.classed("active", function(p) {
			//					return p.data[0] === d[0] //|| p.data[0] === d; 
			//				});
			//			pies3D.svg.selectAll("." + pies3D._pie3D_percent_class)
			//				.style("visibility", function(p) { //.style("visibility", "hidden")
			//					return p.data[0] === d[0] ? "visible" : "hidden" //|| p.data[0] === d; 
			//				})
			//				.style("color", function(p) {
			//					return p.data[0] === d[0] ? "white" : "none";
			//				});
		}

		function mouseout_pies(d, pies3D) {
			pies3D.tooltip.style("opacity", 0);
			pies3D.svg.selectAll("." + pies3D._pie3D_topSlice_class)
				.classed("active", false);
			pies3D.svg.selectAll("." + pies3D._pie3D_innerSlice_class)
				.classed("active", false);
			pies3D.svg.selectAll("." + pies3D._pie3D_outerSlice_class)
				.classed("active", false);
			pies3D.svg.selectAll("." + pies3D._pie3D_percent_class)
				.style("visibility", "hidden");
		}

		function mouseout_legend(d, pies3D) {
			pies3D.tooltip.style("opacity", 0);
			pies3D.svg.selectAll("." + pies3D._pie3D_topSlice_class)
				.classed("active", false);
			pies3D.svg.selectAll("." + pies3D._pie3D_innerSlice_class)
				.classed("active", false);
			pies3D.svg.selectAll("." + pies3D._pie3D_outerSlice_class)
				.classed("active", false);
			//			pies3D.svg.selectAll("." + pies3D._pie3D_percent_class)
			//				.style("visibility", "hidden");
		}

		function getPercent(d) {
			return(d.endAngle - d.startAngle > 0.2 ?
				Math.round(1000 * (d.endAngle - d.startAngle) / (Math.PI * 2)) / 10 + '%' : '');
		}
		return pies3D;
	};
	this.Pies3D = Pies3D;
}();